package com.zhong.kotlin.base


import android.app.Fragment
import android.os.Bundle
import com.hjtech.baselib.mvpbase.BasePresenter
import com.hjtech.baselib.mvpbase.BaseView

/*
 * 项目名:    EasyPark
 * 包名       com.hjtech.easypark.common.base
 * 文件名:    BaseFragment
 * 创建者:    ZSY
 * 创建时间:  2017/6/20 on 14:15
 * 描述:     TODO Fragment基类
 */
abstract class BaseFragment<P : BasePresenter> : Fragment(), BaseView {
    protected var presenter: P? = null
    private var isViewCreate = false//view是否创建
    private var isViewVisible = false//view是否可见
    private var isFirst = true//是否第一次加载


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        presenter = initPresenter()
    }


    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        isViewCreate = true
    }


    override fun setUserVisibleHint(isVisibleToUser: Boolean) {
        super.setUserVisibleHint(isVisibleToUser)
        isViewVisible = isVisibleToUser
        if (isVisibleToUser && isViewCreate) {
            visibleToUser()
        }
    }

    override fun onResume() {
        super.onResume()
        if (isViewVisible) {
            visibleToUser()
        }
    }

    /**
     * 懒加载
     * 让用户可见
     * 第一次加载
     */
    protected fun firstLoad() {

    }

    /**
     * 懒加载
     * 让用户可见
     */
    protected fun visibleToUser() {
        if (isFirst) {
            firstLoad()
            isFirst = false
        }
    }


    override fun onDestroyView() {
        if (presenter != null) {
            presenter!!.detach()
        }
        isViewCreate = false
        super.onDestroyView()
    }

    abstract fun initPresenter(): P


    override fun showLoadingDialog(msg: String) {

    }

    override fun dimissLoadingDialog() {

    }
}
